﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Xml;
using System.Windows.Ink;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Spritehand.FarseerHelper;
using FarseerGames.FarseerPhysics;
using System.Windows.Interactivity;
using Spritehand.PhysicsBehaviors;
using Microsoft.Expression.Interactivity;
using Microsoft.Expression.Interactivity.Layout;

namespace DiagramDesigner
{
    public partial class Window1 : Window
    {
        //物理引擎
        private PhysicsControllerMain _physicsController;
        //标志用
        private int record;
        /// <summary>
        /// 构造函数
        /// </summary>
        public Window1()
        {
            InitializeComponent();
            AddButton();
            record = 0;
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        void AddButton()
        {
            //增加物理效果
            Button addphysics = new Button();
            addphysics.Content = "addphysics";
            addphysics.Click += new RoutedEventHandler(Button_Click);
            Root.Children.Add(addphysics);
            //取出缩放和拖动的效果
            Button makeready = new Button();
            makeready.Content = "makeready";
            Canvas.SetLeft(makeready, 100);
            makeready.Click += new RoutedEventHandler(Button_Click);
            Root.Children.Add(makeready);
           
            //添加矩形
            Button addEllipse = new Button();
            addEllipse.Content = "addellipse";
            Canvas.SetLeft(addEllipse, 300);
            addEllipse.Click += new RoutedEventHandler(Button_Click);
            Root.Children.Add(addEllipse);
            //添加大地
            Button Addplatform = new Button();
            Addplatform.Content = "Addplatform";
            Canvas.SetLeft(Addplatform, 400);
            Addplatform.Click += new RoutedEventHandler(Button_Click);
            Root.Children.Add(Addplatform);
        }


        void Button_Click(object sender, RoutedEventArgs e)
        {
            Button resiver = sender as Button;
            String content = resiver.Content.ToString();
            switch (content)
            {
                case "addphysics":
                    addphysics_Click();
                    break;
                case "makeready":
                    makeready_Click();
                    break;
                case "addellipse":
                    Addellipse();
                    break;
                default:
                    Addplatform_Click();
                    break;
            }
        }

        /// <summary>
        /// 增加物理效果
        /// </summary>
        void addphysics_Click()
        {

            BehaviorCollection behaviorCollection;

            //UserControl NItem;
            for (int i = Root.Children.Count - 1; i >= 0; --i)
            {
                Object newItem = Root.Children[i];
                if (newItem is UserControl)
                {
                    UserControl NItem = newItem as UserControl;

                    NItem.Name = "N" + record.ToString();
                    ++record;
                    // NItem.Name = "yuhang";
                    behaviorCollection = Interaction.GetBehaviors(NItem);
                    behaviorCollection.Add(new PhysicsObjectBehavior());
                    _physicsController = Root.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
                    _physicsController.AddPhysicsBody(NItem.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);
                }
                else if (newItem is Rectangle) {
                    Rectangle platform = newItem as Rectangle;
                    string ss = platform.Name.Substring(0, 8);
                    if (platform.Name.Substring(0,8) == "platform") {
                        behaviorCollection = Interaction.GetBehaviors(platform);
                        PhysicsObjectBehavior POB = new PhysicsObjectBehavior();
                        POB.IsStatic = true;
                        behaviorCollection.Add(POB);
                        _physicsController = Root.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
                        _physicsController.AddPhysicsBody(platform.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);
                    }
                }
            }
        }
        /// <summary>
        /// 取出缩放和拖动的效果
        /// </summary>
        void makeready_Click()
        {
            foreach (Object newItem in Root.Children)
            {
                if (newItem is UserControl)
                {
                    UserControl NItem = newItem as UserControl;
                    NItem.Template = (ControlTemplate)(this.Resources["StaticDesignerItemTemplate"]);
                }
            }
        }
        /// <summary>
        /// 添加椭圆
        /// </summary>
        void addEllipse_Click()
        {
            Addellipse();
        }
        /// <summary>
        /// 添加大地
        /// </summary>
        void Addplatform_Click()
        {
            Rectangle platform = new Rectangle();
            platform.Width = 945;
            platform.Height = 36;
            platform.Name = "platform"+record.ToString();
            Canvas.SetLeft(platform, -122);
            Canvas.SetTop(platform, 468);
            platform.Fill = System.Windows.Media.Brushes.Green;
            Root.Children.Add(platform);
        }
       
        //
        void Addellipse()
        {
            Ellipse ellipse = new Ellipse();
            ellipse.Fill = System.Windows.Media.Brushes.Red;
            ellipse.IsHitTestVisible = false;
            UserControl ellcopy = new UserControl();
            ellcopy.Name = "new";
            ellcopy.Width = 130;
            ellcopy.MinWidth = 50;
            ellcopy.Height = 130;
            ellcopy.MinHeight = 50;
            Canvas.SetTop(ellcopy, 150);
            Canvas.SetLeft(ellcopy, 470);
            ellcopy.Template = (ControlTemplate)(this.Resources["DesignerItemTemplate"]);
            ellcopy.Content = ellipse;
            Root.Children.Add(ellcopy);
        } 
    }
}
